<template>


  <div class="bruce">
    <ul class="bubble-bgwall">
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
    </ul>
  </div>



  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1>后台管理系统</h1>
      </div>

      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input id="username" v-model="username" type="text" placeholder="请输入用户名" required />
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <div class="password-container">
            <input id="password" v-model="password" :type="showPassword ? 'text' : 'password'" placeholder="请输入密码"
              required />
          </div>
        </div>

        <div class="form-footer">
          <div class="remember-me">
            <input id="remember" type="checkbox" v-model="rememberMe" />
            <label for="remember">记住我</label>
          </div>
          <a href="#" class="forgot-password" @click="handleForgotPassword">忘记密码?</a>
        </div>

        <div class="button-container">
          <button type="submit" class="login-button">登录</button>
          <button type="button" @click="handleRegister" class="register-button">注册</button>
        </div>

        <div v-if="error" class="error-message">
          {{ error }}
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { ref, onMounted } from 'vue';
import axios from 'axios'

axios.defaults.baseURL = 'http://localhost:3000';

export default {
  setup() {
    const store = useStore();
    const router = useRouter();

    const username = ref('');
    const password = ref('');
    const rememberMe = ref(false);
    const error = ref('');
    const showPassword = ref(false);

    // 检查是否已有token
    onMounted(() => {
      // 只在当前路由不是登录页时自动跳转
      if (localStorage.getItem('token') && router.currentRoute.value.name !== 'Login') {
        router.push('/dashboard');
      }
    });

    const handleLogin = async () => {
      console.log('Attempting login with:', username.value, password.value); // 调试日志

      try {
        const response = await axios.post('/login', {
          username: username.value,
          password: password.value
        });

        console.log('Login response:', response.data); // 调试日志


        const { token } = response.data;
        store.dispatch('login', {
          username: username.value,
          token
        });

        // 如果选择记住我，设置较长的过期时间
        if (rememberMe.value) {
          // 这里可以设置cookie或其他持久化存储方式
        }

        router.push('/dashboard');
      } catch (err) {

        console.error('Login error:', err); // 错误日志


        error.value = err.response?.data?.message || '登录失败，请检查用户名和密码';
      }
    };

    const handleRegister = () => {
      router.push('/register');
    };

    const handleForgotPassword = () => {
      router.push('/forgotPassword');
    };

    return {
      username,
      password,
      rememberMe,
      error,
      showPassword,
      handleLogin,
      handleRegister,
      handleForgotPassword
    };
  }
};
</script>

<style scoped>
/* 全局样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

body {
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  height: 100vh;
  overflow: hidden;
  /* 防止滚动条出现 */
}

/* 登录容器 - 修改为右下角定位 */
.login-container {
  position: fixed;
  /* 使用固定定位 */
  right: 500px;
  /* 距离右边缘30px */
  bottom: 300px;
  /* 距离底部30px */
  width: 400px;
  /* 固定宽度 */
  z-index: 1000;
  /* 确保在页面最上层 */
}

/* 登录框 */
.login-box {
  background: white;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
  overflow: hidden;
  animation: fadeIn 0.5s ease;
}

/* 登录头部 */
.login-header {
  padding: 30px;
  text-align: center;
  background: #a373ca;
  color: white;
}

.logo {
  width: 60px;
  height: 60px;
  margin-bottom: 15px;
}

.login-header h1 {
  font-size: 24px;
  font-weight: 600;
}

/* 登录表单 */
.login-form {
  padding: 30px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
}

.form-group input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 16px;
  transition: border-color 0.3s;
}

.form-group input:focus {
  border-color: #2475d1;
  outline: none;
}

/* 密码输入框和眼睛图标 */
.password-container {
  position: relative;
}

.eye-button {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: #666;
  padding: 0;
}

.eye-button:hover {
  color: #2475d1;
}

/* 表单底部 */
.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.remember-me {
  display: flex;
  align-items: center;
}

.remember-me input {
  margin-right: 8px;
}

.forgot-password {
  color: #4dcde4;
  text-decoration: none;
  font-size: 14px;
}

.forgot-password:hover {
  text-decoration: underline;
}

/* 按钮容器 */
.button-container {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

/* 登录按钮 */
.login-button {
  flex: 1;
  padding: 12px;
  background: #be9bec;
  color: rgb(96, 63, 216);
  border: none;
  border-radius: 5px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  border: 1px solid #2c9db1;
}

.login-button:hover {
  background: #1a7b8c;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(44, 157, 177, 0.3);
}

/* 注册按钮 */
.register-button {
  flex: 1;
  padding: 12px;
  background: white;
  color: #ac73ec;
  border: 1px solid #2c9db1;
  border-radius: 5px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
}

.register-button:hover {
  background: #f0f9fb;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(44, 157, 177, 0.1);
}

/* 错误消息 */
.error-message {
  margin-top: 20px;
  padding: 10px;
  background: #ffe6e6;
  color: #d32f2f;
  border-radius: 5px;
  text-align: center;
}

/* 动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}



/* 气泡背景样式 */
html, body {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
}

.bruce {
  height: 100%;
  background-image: linear-gradient(270deg, #8146b4, #6990f6);
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: -1;
}

.bubble-bgwall {
  overflow: hidden;
  position: relative;
  width: 100%;
  height: 100%;
  padding: 0;
}

.bubble-bgwall li {
  display: flex;
  position: absolute;
  bottom: -200px;
  justify-content: center;
  align-items: center;
  border-radius: 10px;
  width: 50px;
  height: 50px;
  background-color: rgba(255, 255, 255, 0.15);
  color: #ccc;
  animation: bubble 15s infinite;
}

.bubble-bgwall li:nth-child(1) {
  left: 10%;
}

.bubble-bgwall li:nth-child(2) {
  left: 20%;
  width: 90px;
  height: 90px;
  animation-duration: 7s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(3) {
  left: 25%;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(4) {
  left: 40%;
  width: 60px;
  height: 60px;
  background-color: rgba(255, 255, 255, 0.3);
  animation-duration: 8s;
}

.bubble-bgwall li:nth-child(5) {
  left: 70%;
}

.bubble-bgwall li:nth-child(6) {
  left: 80%;
  width: 120px;
  height: 120px;
  background-color: rgba(255, 255, 255, 0.2);
  animation-delay: 3s;
}

.bubble-bgwall li:nth-child(7) {
  left: 32%;
  width: 160px;
  height: 160px;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(8) {
  left: 55%;
  width: 40px;
  height: 40px;
  font-size: 12px;
  animation-duration: 15s;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(9) {
  left: 25%;
  width: 40px;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.3);
  font-size: 12px;
  animation-duration: 12s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(10) {
  left: 85%;
  width: 160px;
  height: 160px;
  animation-delay: 5s;
}

@keyframes bubble {
  0% {
    opacity: 0.5;
    transform: translateY(0) rotate(45deg);
  }
  25% {
    opacity: 0.75;
    transform: translateY(-400px) rotate(90deg);
  }
  50% {
    opacity: 1;
    transform: translateY(-600px) rotate(135deg);
  }
  100% {
    opacity: 0;
    transform: translateY(-1000px) rotate(180deg);
  }
}
</style>